package adavance.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileClient3 {
    //没有管是否存在。。。
    //改造方案：
    //1:找到文件了，发内容之前，发 1，然后继续发内容；其他问题 发0，线程就结束
    public static void main(String[] args) {
        Socket s=null;
        try {
            s=new Socket("localhost",8001);
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            System.out.println("请输入文件名:");
            String fileName=new Scanner(System.in).next();
            PrintWriter pw=new PrintWriter(out,true);
            pw.println(fileName);
            int read = in.read();
            if (read==0){
                System.out.println("文件不存在");
            }else {
                byte[] b = new byte[10240];
                int len = 0;
                FileOutputStream fout = new FileOutputStream(fileName);
                while ((len = in.read(b)) > 0) {
                    fout.write(b, 0, len);
                }
                fout.close();
                System.out.println("文件接受成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }

    }
}
